package com.tem.system.controller;

import com.api.manage.pojo.system.SysCompanyBean;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.swagger.annotations.ApiOperation;
import org.apache.commons.lang3.time.DateFormatUtils;
import org.springframework.web.bind.annotation.*;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.ui.Model;
import org.springframework.web.servlet.ModelAndView;
import java.util.Date;
import java.util.List;
import java.util.ArrayList;
import com.tem.common.tool.result.ResultUtil;
import org.springframework.beans.BeanUtils;
import com.tem.system.entity.SysDepartment;
import com.api.manage.pojo.system.SysDepartmentBean;
import com.tem.system.service.ISysDepartmentService;
import com.tem.common.core.base.BaseController;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;

/**
 * <p>
 *  前端控制器
 * </p>
 * @author 腾木
 * @since 2023-11-17
 */
@RestController
@RequestMapping("/system/sysDepartment")
public class SysDepartmentController extends BaseController{
    @Autowired
    private ISysDepartmentService sysDepartmentService;

    @ApiOperation(value = "列表")
    @RequestMapping("list")
    public ResultUtil<IPage<SysCompanyBean>> list(Long sysBranchOfficeId,String sysCompanyName, HttpServletRequest req){
        if(sysBranchOfficeId==null){
            sysBranchOfficeId = this.getSysBranchOfficeId(req);
        }
        List<SysDepartmentBean> depts = sysDepartmentService.queryList(sysBranchOfficeId);
        IPage listBody = new Page(1,200,depts.size(),false);
        listBody.setRecords(depts);
        return ResultUtil.success(listBody);
    }

    @PostMapping("save")
    @ApiOperation(value = "新增")
    public ResultUtil save(@RequestBody SysDepartment entity){
        //entity.setCreateTime(LocalDateTime.now());entity.setDeleted(0);
        if(entity.getId()!=null){
            sysDepartmentService.updateById(entity);
        }else{
            entity.setDeleted(0);
            sysDepartmentService.save(entity);
        }
        return ResultUtil.success();
    }

    @GetMapping("getById/{id}")
    @ApiOperation(value = "id查询")
    public ResultUtil<SysDepartmentBean> getById(@PathVariable String id){
        SysDepartment entity = sysDepartmentService.getById(id);
        if(entity!=null){
            SysDepartmentBean bean = new SysDepartmentBean();
            BeanUtils.copyProperties(entity,bean);
            return ResultUtil.success(bean);
        }
        return ResultUtil.error("没有找到对象");
    }

    @PutMapping("update")
    @ApiOperation(value = "修改")
    public ResultUtil update(@RequestBody SysDepartment entity){
        sysDepartmentService.updateById(entity);
        return ResultUtil.success();
    }

    @ApiOperation(value = "删除")
    @DeleteMapping("delete/{id}")
    public ResultUtil delete(@PathVariable Long id){
        sysDepartmentService.removeById(id);
        return ResultUtil.success();
    }
}